package com.rc.evcharger.biz.notify;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rc.evcharger.common.config.RabbitListenerConstant;
import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.common.mq.MqConfig;
import com.rc.evcharger.common.mq.MqProducer;
import com.rc.evcharger.common.mq.MqProducer2;
import com.rc.evcharger.model.elec.ElecGun;
import com.rc.evcharger.model.elec.ElecPile;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.service.HlhtOrderCostService;
import com.rc.evcharger.service.OrderCostService;
import com.rc.evcharger.service.elec.ElecGunService;
import com.rc.evcharger.service.elec.ElecPileService;
import com.rc.evcharger.service.order.OrderInfoService;
import com.rc.evcharger.vo.charge.CommandStopReq;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 6.9 notification_stop_charge_result
 * 推送订单停止充电的结果给应用层
 * Created by Sven on 2021/02/26
 */
@Component
public class NotifyStopChargeResult {
    private static Logger logger = LogManager.getLogger(NotifyStopChargeResult.class);

    @Autowired
    private MqProducer mqProducer;

    @Autowired
    private MqProducer2 mqProducer2;

    @Autowired
    private OrderCostService orderCostService;

    @Autowired
    private ElecGunService elecGunService;

    @Autowired
    private ElecPileService elecPileService;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private HlhtOrderCostService hlhtOrderCostService;

    /**
     * 推送订单停止状态
     */
    public void notifyOrderStop(String orderNum, Integer status, Integer orderStatus, Integer notifyType) {
        OrderInfo orderInfo = orderInfoService.selectByOrderNum(orderNum);

        ElecGun elecGun = elecGunService.selectByPrimaryKey(orderInfo.getElecGunId());
        ElecPile elecPile = elecPileService.selectByPrimaryKey(orderInfo.getElecPileId());

        CommandStopReq commandStopReq = new CommandStopReq();
        commandStopReq.setStatus(status);//订单停止状态 0.停止成功 1.停止失败

        commandStopReq.setTenantCode(orderInfo.getTenantCode());
        commandStopReq.setTenantOperatorCode(orderInfo.getTenantOperatorCode());
        commandStopReq.setHlhtTenantCode(orderInfo.getHlhtTenantCode());
        commandStopReq.setHlhtOperatorId(orderInfo.getHlhtOperatorId());

        commandStopReq.setMemberId(orderInfo.getMemberId());

        commandStopReq.setOrderStatus(orderStatus);
        commandStopReq.setPileStopReason(orderInfo.getPileStopReason());
        commandStopReq.setStopReason(orderInfo.getStopReason());

        commandStopReq.setOrderSource(orderInfo.getOrderSource());
        commandStopReq.setOrderId(orderInfo.getOrderId());
        commandStopReq.setOutOrderNum(orderInfo.getOutOrderNum());
        commandStopReq.setOrderNum(orderInfo.getOrderNum());
        commandStopReq.setGunNo(elecGun.getGunNo());

        commandStopReq.setElecStationId(orderInfo.getElecStationId());
        commandStopReq.setElecPileId(orderInfo.getElecPileId());
        commandStopReq.setElecGunId(orderInfo.getElecGunId());

        commandStopReq.setNotifyType(notifyType);//推送类型 0正常充电 1后台补推送 默认0

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String enData = objectMapper.writeValueAsString(commandStopReq);

            if (status == SocketConstant.CON1) {//停止失败
                commandStopReq.setStatus(status);

                mqProducer.sendDelay(MqConfig.OPEN2APP_NOTIFY_STOP_CHARGE_RESULT, enData, 1000);

                logger.info("notifyOrderStop 停止失败订单推送完成，对应的订单号:{},对应的ordersource:{}", orderInfo.getOrderNum(), orderInfo.getOrderSource());
            } else {
                //消息推送
                mqProducer.sendDelay(MqConfig.OPEN2APP_NOTIFY_STOP_CHARGE_RESULT, enData, 1000);

                logger.info("notifyOrderStop 订单推送完成，对应的订单号:{},对应的ordersource:{}", orderInfo.getOrderNum(), orderInfo.getOrderSource());

                //先付后退
                if ("3".equals(orderInfo.getAccountType())) {
                    mqProducer2.sendDelay(RabbitListenerConstant.PAYMENT_REFUND_ORDER, enData, 1000);
                }
            }

            if ("2".equals(orderInfo.getStatus()) || "4".equals(orderInfo.getStatus())) {
                //todo 清理redis缓存
                orderCostService.clearRedis(elecPile.getPileRtua(), elecGun.getGunNum(), orderNum, orderInfo.getMemberId());
            }
        } catch (Exception e) {
            logger.error("订单推送出错了对应的订单号:" + orderInfo.getOrderNum(), e);
        }
    }
}
